﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO_HealthInsurance;
using BLL_HealthInsurance;
using System.Data;

namespace HealthInsuranceMgm.Admin
{
    public partial class ListCompanyInsurence : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
            if(!IsPostBack){
               
                load();
                PanelUpdatecompany.Visible = false;
                PanelListCompany.Visible = true;
            }
        }

        public void load()
        {
            BLLCompany bll = new BLLCompany();
            GridView1.DataSource = bll.ListCompany();
            GridView1.DataBind();
        }
        public void LoadData(int CompanyID)
        {
            BLLCompany obj = new BLLCompany();
            DTOCompanyDetail objDt = new DTOCompanyDetail();
            objDt.CompanyId = CompanyID;
            DataTable dt = obj.GetCompanyById(objDt);
            HiddenField2.Value = dt.Rows[0]["CompanyId"].ToString();
            txtCompanyName.Text = dt.Rows[0]["CompanyName"].ToString();
            txtCompanyAddress.Text = dt.Rows[0]["Address"].ToString();
            txtCompanyPhone.Text = dt.Rows[0]["Phone"].ToString();
            txtUrlCompany.Text = dt.Rows[0]["CompanyUrl"].ToString();


        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            BLLCompany obj = new BLLCompany();
            DTOCompanyDetail objDt = new DTOCompanyDetail();
            if (e.CommandName == "Delete")
            {
                // get the categoryID of the clicked row
                int categoryID = Convert.ToInt32(e.CommandArgument);
                // Delete the record 
                objDt.CompanyId = categoryID;
                obj.deleteCompany(objDt);
                // Implement this on your own :) 
            }
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {   
            int CompanyID = Int32.Parse(GridView1.DataKeys[e.NewSelectedIndex].Value.ToString());
            LoadData(CompanyID);
            PanelListCompany.Visible = false;
            PanelUpdatecompany.Visible = true;
        }
        public SortDirection SortDirection
        {
            get
            {

                if (ViewState["SortDirection"] == null)
                {
                    ViewState["SortDirection"] = SortDirection.Ascending;
                }
                return (SortDirection)ViewState["SortDirection"];
            }
            set
            {
                ViewState["SortDirection"] = value;
            }
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
            BLLCompany obj = new BLLCompany();
            string sortExpression = e.SortExpression;
            string Direction = "";

            if (SortDirection == SortDirection.Ascending)
            {
                SortDirection = SortDirection.Descending;
                Direction = "DESC";
            }
            else
            {
                SortDirection = SortDirection.Ascending;
                Direction = "ASC";
            }
            DataTable dt = obj.ListCompany();
            dt.DefaultView.Sort = sortExpression + " " + Direction;
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript: return " +
                "confirm('Are you sure you want to delete this record " +
                DataBinder.Eval(e.Row.DataItem, "CompanyId") + "')");
              
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int companyID = (int)GridView1.DataKeys[e.RowIndex].Value;
            BLLCompany obj = new BLLCompany();
            DTOCompanyDetail g = new DTOCompanyDetail();
            g.CompanyId = companyID;
            obj.deleteCompany(g);
            load();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            load();
        }
        public void message(string script)
        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", script, true);
        }
        protected void btnupdate_Click(object sender, EventArgs e)
        {
            string companyName = txtCompanyName.Text;
            string address = txtCompanyAddress.Text;
            string phone= txtCompanyPhone.Text;
            string url = txtUrlCompany.Text;
            if(IsValid){
                DTOCompanyDetail objCom = new DTOCompanyDetail();
                BLLCompany objbll = new BLLCompany();
                objCom.CompanyId = Int32.Parse(HiddenField2.Value.ToString());
                objCom.CompanyName = companyName;
                objCom.Address = address;
                objCom.Phone = phone;
                objCom.CompanyUrl = url;
                objbll.updateCompany(objCom);
                message("alert('Update Successfull');");
                //dskkkkkkkkkkkkkkkkkkkkkkkf
            }
        }
    }
}